package com.arch.dev.server.servlet;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.OutputStream;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

/**
 * 
 * Servlet implementation class PdfServlet
 */

public class PdfServlet extends HttpServlet {
	
	Logger logger = Logger.getLogger(PdfServlet.class);	

	private static final long serialVersionUID = 1L;

	/**
	 * 
	 * Default constructor.
	 */

	public PdfServlet() {

		// TODO Auto-generated constructor stub

	}

	/**
	 * 
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */

	/*protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		OutputStream out = response.getOutputStream();

		response.setContentType("application/pdf");

		String pdfName = request.getParameter("pdfname");

		response.setHeader(

		"Content-disposition",

		"attachment; filename=" + pdfName + ".pdf");

		System.out.println("From servlet" + pdfName);

		ServletContext sc = this.getServletContext();

		System.out.println("*********" + sc.getRealPath("/"));

		File directory = new File(sc.getRealPath("/") + "PDF");

		File file = new File(directory, pdfName + ".pdf");

		System.out.println("*****" + file.getAbsolutePath());

		// File file = new File("PDF/" + pdfName + ".pdf");

		FileInputStream fin = new FileInputStream(file);

		byte[] b = new byte[1024];

		while (fin.read(b) != -1) {

			out.write(b, 0, b.length);

		}

		fin.close();

		out.flush();

		out.close();

	}
	*/
	enum FileType{EXCEL,PDF};
	
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		int lastIndex = request.getRequestURI().lastIndexOf('/');
		
		String fileExtn = request.getRequestURI().substring(lastIndex+1);
		
		logger.info(fileExtn);

		FileType fileType ;
		
		if(fileExtn.equalsIgnoreCase("pdf")){
			fileType = FileType.PDF;
		}else{
			fileType = FileType.EXCEL;
		}
		String extn = "";
		String dir = "";
		switch (fileType) {
		case EXCEL:
			response.setContentType("application/excel");
			extn = ".xls";
			dir = "Excel";
			break;
		case PDF:
			response.setContentType("application/pdf");
			extn = ".pdf";
			dir = "PDF";
			break;
		default:
			break;
		}
		OutputStream out = response.getOutputStream();	
		String fileName = request.getParameter("fileName");

		response.setHeader(

		"Content-disposition",

		"attachment; filename=" + fileName + extn);

		logger.info("From servlet" + fileName);

		ServletContext sc = this.getServletContext();

		logger.info("*********" + sc.getRealPath("/"));

		File directory = new File(sc.getRealPath("/") + dir);

		File file = new File(directory, fileName + extn);

		logger.info("*****" + file.getAbsolutePath());

		// File file = new File("PDF/" + pdfName + ".pdf");

		FileInputStream fin = new FileInputStream(file);

		byte[] b = new byte[1024];

		while (fin.read(b) != -1) {

			out.write(b, 0, b.length);

		}

		fin.close();

		out.flush();

		out.close();

	}

	/**
	 * 
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

	}

}
